package com.example.myapplication.z_b_details

import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.viewpager.widget.ViewPager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.mvp.view.BaseActivity
import com.example.myapplication.R
import com.example.myapplication.adapter.Details_Adapter
import com.example.myapplication.entity.Details_Entity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.HttpType
import com.google.android.material.tabs.TabLayout

class DetailsActivity :BaseActivity<LjlPresenter>(),LjlContract.LjlView {
    private val details: Toolbar by lazy { findViewById<Toolbar>(R.id.details) }
    private val detailsTab: TabLayout by lazy { findViewById<TabLayout>(R.id.details_tab) }
    private val detailsVip: ViewPager by lazy { findViewById<ViewPager>(R.id.details_vip) }
    private val detailsRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.details_rec) }
    private var adapter: Details_Adapter?=null

    private var params: ArrayList<Map<String,Any>>?= null

    override fun findlayout(): Int {
        return R.layout.activity_details
    }

    override fun initView() {
        params = ArrayList()

        var name = intent.getStringExtra("name")
        details.title = name
        details.setTitleTextColor(Color.WHITE)
        details.setNavigationOnClickListener(object :View.OnClickListener{
            override fun onClick(v: View?) {
                finish()
            }
        })




    }

    override fun initData() {
        mypre = LjlPresenter(LjlModel(),this)
        var map:HashMap<String,Any>?= HashMap()
        map!!.put("code",HttpType.DETAILS)
        map!!.put("brand","1")
        map!!.put("page","1")
        params!!.add(map)

        mypre.TabData(params!!)

        detailsTab.addTab(detailsTab.newTab().setText("默认"))
        detailsTab.addTab(detailsTab.newTab().setText("人气"))
        detailsTab.addTab(detailsTab.newTab().setText("新品"))
        detailsTab.addTab(detailsTab.newTab().setText("价格"))
        detailsTab.addTab(detailsTab.newTab().setText("筛选"))
        //设置间隔线
        var childAt = detailsTab.getChildAt(0)
        var linearLayout = childAt as LinearLayout

        linearLayout.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE
        linearLayout.dividerDrawable = ContextCompat.getDrawable(this,R.drawable.tab_xian)
        linearLayout.dividerPadding = 3

    }

    override fun showmsg(entity: BaseEntity) {
        if (entity is Details_Entity){
            var detailsEntity = entity as Details_Entity
            var values = detailsEntity.values?.get(0)!!.goods


            adapter = Details_Adapter(R.layout.details_item,values!!)
            detailsRec.adapter = adapter
            detailsRec.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

            adapter!!.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                override fun onItemClick(
                    adapter: BaseQuickAdapter<*, *>?,
                    view: View?,
                    position: Int
                ) {
                    var intent = Intent(this@DetailsActivity,BuyActivity::class.java)
                    intent.putExtra("imgpath",values[position].goods_img_path)

                    startActivity(intent)
                }

            })
        }



    }




}
